<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/7/23
 * Time: 14:26
 */
//ini_set("display_errors", "on");
//error_reporting(E_ALL);
include_once IA_ROOT.'/addons/jy_signup_a/functions.php';
defined('IN_IA') or exit('Access Denied');
global $_W,$_GPC;
$ops = array("modify","ajaxnumberrule");
$op = in_array($_GPC['op'],$ops) ? $_GPC['op'] : 'modify';
$table = "aide_activity_number_rule";
$letters = array('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z');
$figure = 4; // 数字位数，自定义，必须和前台对应

/*异步请求字段号码规则*/
if($op == 'ajaxnumberrule'){
    $fieldid = $_GPC['fieldid'];

    $options = pdo_getall("aide_activity_pro",array("aid"=>$fieldid));
//    print_r($options);exit;
    include aide_template("ajaxnumberrule");
}
/*编辑号码规则*/
if($op == 'modify'){
    if($_W['isajax']){
        $post = $_POST;
        if($_W['token']<>$post['token'] || !($post['id']>0)) exitjson(0, '参数错误');
        $nr = pdo_get($table,array("id" => $post['id']));
        if(!$nr) exitjson(0, "操作错误，请返回活动列表重试");
        if($post['status'] == 1){
            if(!empty($post['zimu']) && is_array($post['zimu'])){
                $zimu = $post['zimu'];
                $min_num = $post['min_num'];
                $max_num = $post['max_num'];
                if(count($zimu) != count(array_unique($zimu))) exitjson(0, "首字母不能有重复，请重新选择");
                $number_rule = array();
                for($i=0; $i<count($zimu); $i++){
                    if(!($min_num[$i]>0) || !($max_num[$i]>0)) exitjson(0, "请输入1 ~ ".str_repeat('9',$figure)."的数字");
                    $min_num[$i] = sprintf("%0{$figure}d", substr($min_num[$i],0,$figure));
                    $max_num[$i] = sprintf("%0{$figure}d", substr($max_num[$i],0,$figure));
                    $number_rule[$zimu[$i]][0] = $min_num[$i];
                    $number_rule[$zimu[$i]][1] = $max_num[$i];
                }
                $number_rule = json_encode($number_rule);
                $data['status'] = 1;
                $data['pid'] = '';
                $data['number_rule'] = $number_rule;
                $res = pdo_update($table, $data, array('id' => $nr['id']));
                if($res !== false){
                    exitjson(1, "操作成功");
                }else{
                    exitjson(0, "操作失败");
                }
            }else{
                exitjson(0, "请添加规则");
            }
        }elseif($post['status'] == 2){
            $fieldid = $post['aid'];
            $options = pdo_getall("aide_activity_pro",array("aid"=>$fieldid));
            if(!$options) exitjson(0, "该字段无选项");
            /*参数判断*/
            foreach($post as $key=>$val){
                if(strpos($key, 'zimu_') !== false){
                    foreach($val as $k=>$v){
                        $zimuall[] = $v;
                    }
                }
                if(strpos($key, 'min_num_') !== false){
                    foreach($val as $k=>$v){
                        if(!($v > 0)){ exitjson(0, "请输入1 ~ ".str_repeat('9',$figure)."的数字"); }
                    }
                }
                if(strpos($key, 'max_num_') !== false){
                    foreach($val as $k=>$v){
                        if(!($v > 0)) exitjson(0, "请输入1 ~ ".str_repeat('9',$figure)."的数字");
                    }
                }
            }
            if(count($zimuall) != count(array_unique($zimuall))) exitjson(0, "首字母不能有重复，请重新选择");
            /*规则转换*/
            $number_rule = array();
            foreach($options as $key=>$val){
                $temp = array();
                $zimu = $post['zimu_'.$val['id']];
                $min_num = $post['min_num_'.$val['id']];
                $max_num = $post['max_num_'.$val['id']];
                for($i=0; $i<count($zimu); $i++){
                    $temp[$zimu[$i]][0] = sprintf("%0{$figure}d", substr($min_num[$i],0,$figure));
                    $temp[$zimu[$i]][1] = sprintf("%0{$figure}d", substr($max_num[$i],0,$figure));
                }
                $number_rule[$val['id']] = $temp;

            }


            /*入库*/
            $data['status'] = 2;
            $data['number_rule'] = json_encode($number_rule);
            $data['pid'] = $fieldid;
            $res = pdo_update($table, $data, array('id' => $nr['id']));
            if($res !== false){
                exitjson(1, "操作成功");
            }else{
                exitjson(0, "操作失败");
            }
        }elseif($post['status'] == 0){
            $data['status'] = 0;
            $res = pdo_update($table, $data, array('id' => $nr['id']));
            if($res !== false){
                exitjson(1, "操作成功");
            }else{
                exitjson(0, "操作失败");
            }
        }
    }else{
        $aid = $_GPC['activityid'];
        $activity = pdo_get("aide_activity",array("id" => $aid));

        if(!$activity) {message("操作错误！，请返回重试1");}
        $condition['aid'] = $aid;
        $nr = pdo_get($table,$condition);
        if(!$nr){
            $data['aid'] = $aid;
            $data['uniacid'] = $_W['uniacid'];
            $data['status'] = 0;
            $res = pdo_insert($table,$data);
            if($res){
                $nr = pdo_get($table,$condition);
            }else{
                message("操作错误！，请返回重试2");
            }
        }
        $number_rule = $number_rule1 = $number_rule2 = array();
        if(!empty($nr['number_rule']) && is_array(json_decode($nr['number_rule'], true))){
            $number_rule = json_decode($nr['number_rule'], true);
            $arrayLevel = arrayLevel($number_rule); //判断2维还是3维
            if($nr['status']==1 || $arrayLevel==2) $number_rule1=$number_rule;
            if($nr['status']==2 || $arrayLevel==3){
                $number_rule2=$number_rule;
                $res = pdo_getall("ims_aide_activity_pro",array("aid"=>$aid));
                foreach($res as $k=>$v){
                    $options[$v['id']] = $v;
                }
            }
        }
        //查找下拉字段
        $attrcondition['aid'] = $aid;
        $xialafield = pdo_getall("ims_aide_activity_pro",$attrcondition);


        include aide_template('numberrule');
        exit;
    }
}

